<?php
//global variables
$_month_names = array('','Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sept','Okt','Nov','Dis');
$_month_lgnames = array('','Januari','Februari','Mac','April','Mei','Jun','Julai','Ogos','September','Oktober','November','Disember');
$_range_months = array();
for ($i=1;$i<=12;$i++) {
$k = $_month_names[$i];
	$_months["$k"] = $i;
}

$weekday_arr = array( 'Monday'    => 'Isnin',
                      'Tuesday'   => 'Selasa',
                      'Wednesday' => 'Rabu',
                      'Thursday'  => 'Khamis',
                      'Friday'    => 'Jumaat',
                      'Saturday'  => 'Sabtu',
                      'Sunday'    => 'Ahad',
                      'Holiday'   => 'Cuti'
);
$weekdays = array('Sunday',
                  'Monday',
				  'Tuesday',
				  'Wednesday',
				  'Thursday',
				  'Friday',
				  'Saturday'
				  );


$_genders = array("Male" => "Lelaki", "Female" => "Perempuan");


function alert_flash_message() {
	if ($_SESSION['flash'] != '') {
	$str = "<center>".
           "<span id=\"flash\" style=\"width:30%; background-color: #009933; color: white; font-weight: ; padding: 4px;\">".
		   $_SESSION['flash'].
		   "</span></center>";
	echo $str;
	$_SESSION['flash'] = '';
	}
}

function alert_debug($msg) {
	$ENABLE_DEBUG = 1;
	if ($ENABLE_DEBUG == 1) {
		echo "<script>alert('$msg')</script>";
	}
}

function dmy_date($ymd_date) {
	if ($ymd_date!='') {
		$d_arr = split("-",$ymd_date);
		return "$d_arr[2]-$d_arr[1]-$d_arr[0]";
	}
	else
		return '';
}

function ymd_date($dmy_date) {
	
	if ($dmy_date!='') {
		$d_arr = split("-",$dmy_date);
		return "$d_arr[2]-$d_arr[1]-$d_arr[0]";
	}
	else
		return '';
}

function mdytoymd_date($mdy_date) {
	//05/13/1952 m d y
	//return substr($mdy_date,6,4) ."-".substr($mdy_date,0,2) ."-".substr($mdy_date,3,2);
	if ($mdy_date!='') {
		$d_arr = split("-",$mdy_date);
		return "$d_arr[2]-$d_arr[0]-$d_arr[1]";
	}
	else
		return '';
}

function exitError($msg) {
	$btn = "<br><input type=button onclick='history.back()' value='<<Back'>";
	$m = "<font color=red>$msg</red> $btn";
	$m = "<center>$m</center>";
	echo $m;exit;
}

function isIncluded($e,$arr) {
	for ($i=0;$i<count($arr);$i++) {
		if ($arr[$i]==$e) return 1;
	}
	return 0;
}

function getHashedObject($fld,$pkey_field,$tbl) {
	global $DB;

	$sql = "SELECT distinct($fld),$pkey_field FROM $tbl";
	$res = pg_query($sql);
	$num = pg_num_rows($res);
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		$arr[] = $dat;
	}
	return $arr;
}

function getList($fld,$tbl) {
	global $DB;

	$sql = "SELECT distinct($fld) FROM $tbl";
	$res = pg_query($sql);
	$num = pg_num_rows($res);
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		$arr[] = $dat[$fld];
	}
	return $arr;
}

function makeIntegerHash($num_start,$num_end) {
	$arr = array();
	for ($i=$num_start;$i<=$num_end;$i++) {
		$arr[] = $i;
	}
	return $arr;
}

function getOptionsFromTable($tblname,$key,$value='') {
	global $DB;

	if ($value!='') $sql = "SELECT distinct($key),$value FROM $tblname";
	else $sql = "SELECT distinct($key) FROM $tblname";
	$res = pg_query($sql);
	$num = pg_num_rows($res);
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		if ($value!='') {
			$k = $dat[$value];
			$v = $dat[$key];
			$arr[$k] = $v;
		}
		else {
			$v = $dat[$key];
			$arr[] = $v;
		}
	}
	return $arr;
}

function getOptionsFromTableWithConds($tblname,$key,$value,$where) {
	global $DB;

	$sql = "SELECT distinct($key),$value FROM $tblname $where";
	$res = pg_query($sql);
	$num = pg_num_rows($res);
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		if ($value!='') {
			$k = $dat[$value];
			$v = $dat[$key];
			$arr[$k] = $v;
		}
		else {
			$v = $dat[$key];
			$arr[] = $v;
		}
	}
	return $arr;
}

function getDistinctValueAsList($val) {
	global $DB;

	$sql = "SELECT distinct($val) FROM vw_lawatan_flat ORDER BY $val";
	$res = pg_query($sql);
	$num = pg_num_rows($res);
	$arr = array();
	for ($i=0;$i<$num;$i++) {
		$dat = pg_fetch_array($res,$i);
		$arr[] = $dat[$val];
	}
	return $arr;
}

//flash some message
function alert_some_message() {
	global $DB;

	$flash = $_SESSION['flash'];

	if ($flash!='') {
		$str = '<center><div style=" text-align:center; background-color: #ccffcc; border-style: solid; border-width:thin; border-color: green; padding: 5px; margin-bottom:10px;">';
	 
		$str .= "$flash\n";
		$str .= "</div></center>";
		echo $str;
	}
	
	$_SESSION['flash'] = '';	
}

//flash some error
function alert_some_error() {
	global $DB;

	$error = $_SESSION['error'];

	if ($error!='') {
		$str = '<center><div style=" text-align:center; background-color: #ffcccc; border-style: solid; border-width:thin; border-color: red; padding: 5px; margin-bottom:10px;">';
	 
		$str .= "$error\n";
		$str .= "</div></center>";
		echo $str;
	}
	
	$_SESSION['error'] = '';	
}

function getObjectCategoryIdFromName($table_name) {
	global $DB;

	$sql = "SELECT * FROM object_categories WHERE table_name ilike '$table_name'";
	$res = pg_query($sql);
	$dat = pg_fetch_object($res,0);
	return $dat->id;
}

function getNameFromId($table_name,$id) {
	global $DB;

	if ($id == '') return '';
	$sql = "SELECT name FROM $table_name WHERE id = '$id'";
	$res = pg_query($sql);
	$dat = pg_fetch_object($res,0);
	return $dat->name;	
}

function getFieldFromId($table_name,$field_name,$id) {
	global $DB;

	if ($id == '') return '';
	$sql = "SELECT $field_name FROM $table_name WHERE id = '$id'";
	$res = pg_query($sql);
	if (pg_num_rows($res) < 1) return '';
	$dat = pg_fetch_object($res,0);
	return $dat->$field_name;	
}

function getSchooNameFromId($table_name,$field_name,$id) {
	global $DB;
	if ($id == '') return '';
	$sql = "SELECT $field_name FROM $table_name WHERE institusi_id = '$id'";
	$res = pg_query($sql);
	if (pg_num_rows($res) < 1) return '';
	$dat = pg_fetch_object($res,0);
	return $dat->$field_name;	
}

function getFieldByWhereSql($table_name,$field_name,$where_clause) {
	global $DB;
	$sql = "SELECT $field_name FROM $table_name $where_clause";
	$res = pg_query($sql);
	$dat = pg_fetch_object($res,0);
	return $dat->$field_name;	
}


function getStates() {
	global $DB;
	$sql = "SELECT * FROM states ORDER BY id";
	$res = pg_query($sql);
	$hash = array();
	while ($dat = pg_fetch_object($res)) {
		$hash[(string)$dat->id] = $dat->name;
	}
	return $hash;	
}

function getItemsOfGroup($group_id) {
	global $DB;
	$sql = "SELECT * FROM items WHERE item_group_id='$group_id' ORDER BY id";
	$res = pg_query($sql);
	$hash = array();
	while ($dat = pg_fetch_object($res)) {
		$hash[(string)$dat->id] = $dat->name;
	}
	return $hash;
}

function getChildSumOfItemByState($item_id, $state_id, $year) {
	global $DB;
	$sql = "SELECT sum(the_value), item_id FROM statistics WHERE item_id='$item_id' AND state_id='$state_id' AND date_part('year',the_date)='$year' GROUP BY item_id";
	$res = pg_query($sql);
	if (pg_num_rows($res) > 0) {
		$dat = pg_fetch_object($res,0);
		return $dat->sum;
	}
	else {
		return '0';
	}
}

function getClassCapacity($kelas_id) {
	global $DB;
	$sql = "SELECT id FROM vw_murid WHERE current_kelas_id='$kelas_id'";
	$res = pg_query($sql);
	$q = pg_num_rows($res);
	return $q;
}

function getClassNameById($kelas_id) {
	global $DB;
	if (empty($kelas_id)) return '';
	$sql = "SELECT * FROM master_kelas WHERE id='$kelas_id'";
	$res = pg_query($sql);
	$dat = pg_fetch_object($res);
	return $dat->nama_kelas;
}

function getKohortUmur($ic_no) {
	//$a = Date("Y")-(1900 + substr($ic_no,0,2));
	$a = Date("y")-(substr($ic_no,0,2));
	
	return $a;
}

function getTotalKohortUmur($kelas_id,$umur) {
	global $DB;
	$ceiling = $umur+1;
	$sql = "SELECT murid_id FROM vw_murid 
			WHERE current_kelas_id='$kelas_id' and age(tarikh_lahir) > $umur and age(tarikh_lahir) < $ceiling";
					
	if (!$res = $DB->Execute($sql)) {
		return 0;
	}
	/*oracle!
	select ROUND ( (SYSDATE - tarikh_lahir ) /365, 0 ) "AGE",
	tarikh_lahir,nama from murid m inner join master_profil p on p.id=m.master_profile_id;	
	*/
	
	return $res->RecordCount();
}

function getOtherTotalKohortUmur($kelas_id,$umur) {
	global $DB;
	$ceiling = 6;
	$sql = "SELECT murid_id FROM vw_murid 
			WHERE current_kelas_id='$kelas_id' and age(tarikh_lahir) < 4 and age(tarikh_lahir) > 6";
					
	if (!$res = $DB->Execute($sql)) {
		return 0;
	}
	return $res->RecordCount();
}

function getClassOfTeacher($guru_id,$the_year) {
	global $DB;
	$sql = "SELECT * FROM master_kelas 
			WHERE guru_id='$guru_id'";
	$res = pg_query($sql);
	$arr = array();
	while($dat = pg_fetch_object($res)) {
		$arr[] = $dat->nama_kelas;
	}
	return join(", ",$arr);
}

function getClassOfPPM($pembantu_id,$the_year) {
	global $DB;
	$sql = "SELECT * FROM master_kelas 
			WHERE pembantu_id='$pembantu_id'";
	$res = pg_query($sql);
	$arr = array();
	while($dat = pg_fetch_object($res)) {
		$arr[] = $dat->nama_kelas;
	}
	return join(", ",$arr);
}

function getTotalClassOfSchool($institusi_id) {
	global $DB;
	$sql = "SELECT count(*) FROM master_kelas 
			WHERE institusi_id='$institusi_id'";
	$res = pg_query($sql);
	return pg_num_rows($res);
}

function countTotalKelas($institusi_id) {
	global $DB;
	$sql = "SELECT count(*) FROM master_kelas 
			WHERE institusi_id='$institusi_id'";
	if (!$res = $DB->Execute($sql)) {
		return 0;
	}
	return $res->RecordCount();

}


?>
